package com.asia5b.a5mall.member.adapter

import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.member.R
import com.asia5b.a5mall.member.databinding.ItemCommentlistAdapterBinding
import com.asia5b.a5mall.middleware.model.member.CommentsModel
import com.asia5b.a5mall.middleware.model.order.OrderDetailModel
import com.asia5b.a5mall.middleware.model.bean.CommentMultiItemEntity
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.utils.*
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class CommentListAdapter(list: ArrayList<CommentMultiItemEntity>, var state: Int = 1) :
        BaseMultiItemQuickAdapter<CommentMultiItemEntity, BaseDataBindingHolder<*>>(list) {
    //state 状态 1：待评价订单,2:待追评,3:已评价
    init {
        addItemType(CommentMultiItemEntity.COMMENT_ORDERWAIT, R.layout.item_commentlist_orderwait_adapter)
        addItemType(CommentMultiItemEntity.COMMENT_WAIT_AGAIN, R.layout.item_commentlist_adapter)
        addItemType(CommentMultiItemEntity.COMMENT_COMMENTED, R.layout.item_commentlist_adapter)

        addChildClickViewIds(R.id.tv_commentlist_watch)
    }

    override fun convert(helper: BaseDataBindingHolder<*>, item: CommentMultiItemEntity) {


        when (item.type) {
            CommentMultiItemEntity.COMMENT_ORDERWAIT -> {
                setOrderWaitView(helper, item as OrderDetailModel)
            }
            CommentMultiItemEntity.COMMENT_WAIT_AGAIN -> {
                setWaitAgainView(helper, item as CommentsModel)
            }
            CommentMultiItemEntity.COMMENT_COMMENTED -> {
                setWaitAgainView(helper, item as CommentsModel)
            }
        }

    }

    /**
     * 待评论
     */
    fun setOrderWaitView(helper: BaseViewHolder, item: OrderDetailModel) {
        val recyclerView = helper.getView<RecyclerView>(R.id.rv_orderwait)
        val tv_commentlist_cnt = helper.getView<TextView>(R.id.tv_commentlist_cnt)
        val tv_orderwait_comment = helper.getView<TextView>(R.id.tv_orderwait_comment)
        val tv_orderwait_ordersn = helper.getView<TextView>(R.id.tv_orderwait_ordersn)
        val tv_orderwait_ordertime = helper.getView<TextView>(R.id.tv_orderwait_ordertime)

        tv_orderwait_ordersn.setText(item.sn)
        if (!item.order_sku_list.isNullOrEmpty()) {
            val commentChildAdapter = CommentChildAdapter(arrayListOf(item.order_sku_list!![0]))
            recyclerView.layoutManager = LinearLayoutManager(context)!!
            recyclerView.adapter = commentChildAdapter
        }
        tv_orderwait_comment.setOnClickListener {
            if (context is AppCompatActivity) {
                (context as AppCompatActivity).push(ActCons.PostCommentActivity, {
                    it.withSerializable("data", item)
                })
            }
        }
        val time = WTimeUtils.format("${item.create_time * 1000}", WTimeUtils.text_yyyy_MM_dd_HH_mm_ss)
        tv_orderwait_ordertime.setText(time)
        tv_commentlist_cnt.text = String.format(ResString.get(R.string.您有商品可以评价2), item.order_sku_list?.size
                ?: 0)


    }

    /**
     * 追加评论/查看评论
     */
    fun setWaitAgainView(helper: BaseDataBindingHolder<*>, item: CommentsModel) {
        val binding = helper.dataBinding as ItemCommentlistAdapterBinding
        binding.model = item
        val tv_commentlist_watch = helper.getView<TextView>(R.id.tv_commentlist_watch)
        tv_commentlist_watch.text = (state == 2).judge(ResString.get(R.string.追加评论), ResString.get(R.string.查看评论))

        val rb_commentlist_star = helper.getView<RatingBar>(R.id.rb_commentlist_star)
        val tv_commentlist_starvalue = helper.getView<TextView>(R.id.tv_commentlist_starvalue)
        when (item.grade) {
            "good" -> {
                rb_commentlist_star.rating = 5.0f
                tv_commentlist_starvalue.text = "5.0"
            }
            "neutral" -> {
                rb_commentlist_star.rating = 3.0f
                tv_commentlist_starvalue.text = "3.0"
            }
            else -> {
                rb_commentlist_star.rating = 1.0f
                tv_commentlist_starvalue.text = "1.0"
            }
        }

    }

    class CommentChildAdapter(list: ArrayList<AbsGoodsModel>) :
            BaseQuickAdapter<AbsGoodsModel, BaseViewHolder>(R.layout.item_commentlist_orderwait_child, list) {
        override fun convert(helper: BaseViewHolder, item: AbsGoodsModel) {
            val iv_orderwait_img = helper.getView<ImageView>(R.id.iv_orderwait_img)
            val tv_orderwait_name = helper.getView<TextView>(R.id.tv_orderwait_name)
            val tv_orderwait_comment = helper.getView<TextView>(R.id.tv_orderwait_comment)
            val tv_orderwait_price = helper.getView<TextView>(R.id.tv_orderwait_price)

            WImageLoader.displayRoundImage(item.absGoodsImageUrl, iv_orderwait_img, 5)
            tv_orderwait_name.setText(item.absGoodsName)

            CurrencyPriceBuilder3().run {
                textView = tv_orderwait_price
                price = item.absGoodsPrice
                priceList = item.absGoodsPriceList
                build14_18()
            }


        }
    }
}